'use strict';

const BaseService = require('./base');

class CommentService extends BaseService {
  get _model() {
    return 'Comment';
  }

  async findOne({ id, houseId }) {
    const { app } = this;
    const where = this.formatWhere({ exact: { id, houseId } });
    const include = [
      {
        model: app.model.House,
        attributes: {
          exclude: app.exclude,
        },
        include: [
          {
            model: app.model.Area,
            attributes: [ 'name' ],
          },
        ],
      },
      {
        model: app.model.User,
        attributes: {
          exclude: app.exclude.concat([ 'password' ]),
        },
        include: [
          {
            model: app.model.UserInfo,
            attributes: [ 'avatar' ],
          },
        ],
      },
    ];
    return this._findOne({ where, include });
  }

  async create({ title, content, pictures, uid, houseId, status }) {
    return this._create({
      title, content, pictures, uid, houseId, status,
    });
  }

  async update({ title, content, pictures, status, id }) {
    const where = { id };
    const body = { title, content, pictures, status };
    return this._update({ body, where });
  }

  async destory(id) {
    const where = { id };
    return this._destory({ where });
  }

  async findList({ title, status, houseId, uid, pageIndex, pageSize, createdAt, prop, sort }) {
    const { app } = this;
    const where = this.formatWhere({ dim: { title }, range: { createdAt }, exact: { status, houseId, uid } });
    const order = this.formatOrder(prop, sort);
    const include = [
      {
        model: app.model.House,
        attributes: [ 'address', 'name' ],
      },
      {
        model: app.model.User,
        attributes: [ 'nickName' ],
        include: [
          {
            model: app.model.UserInfo,
            attributes: [ 'avatar' ],
          },
        ],
      },
    ];
    return this._findList({ pageIndex, pageSize, where, order, include });
  }
}

module.exports = CommentService;
